﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region User
namespace backoffice.Data
{
    partial class User
    {
        public void setUser(User u)
        {
            this.UserID = u.UserID;
            this.Email = u.Email;
            this.Password = u.Password;
            this.Permission = u.Permission;
            this.Name = u.Name;
            this.CompanyName = u.CompanyName;
            this.Address = u.Address;
            this.City = u.City;
            this.PostalCode = u.PostalCode;
            this.RegionID = u.RegionID;
            this.Phone = u.Phone;
            this.RegistrationDate = u.RegistrationDate;
            this.PurchaseDate = u.PurchaseDate;
            this.State = u.State;
        }
    }
}
#endregion

#region dataUser
namespace backoffice.Data
{
    public class dataUser
    {
        #region Create
        public static void createUser(User u)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                u.UserID = Guid.NewGuid();
                u.RegistrationDate = DateTime.Now;

                db.Users.InsertOnSubmit(u);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static User getUser(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Users.FirstOrDefault(u => u.UserID == guid);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static User getUser(Expression<Func<User, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Users.Where(whereClause).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<User> getUsers(Expression<Func<User, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Users.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeUser(User u)
        {
            try
            {
                if (u == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    User res = db.Users.FirstOrDefault(user => user.UserID == u.UserID);
                    if (res == null) return false;
                    else
                    {
                        res.setUser(u);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteUser(User u)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Users.Contains(u))
                {
                    User res = db.Users.FirstOrDefault(user => user.UserID == u.UserID);

                    if (res == null) return false;
                    else
                    {
                        // Update sells
                        IQueryable<Sell> s = db.Sells.Where((Sell sell) => sell.UserID == res.UserID);
                        db.Sells.DeleteAllOnSubmit(s);

                        // Update buys
                        IQueryable<Buy> b = db.Buys.Where((Buy buy) => buy.UserID == res.UserID);
                        db.Buys.DeleteAllOnSubmit(b);

                        // Update experts
                        IQueryable<Expert> experts = db.Experts.Where((Expert ex) => ex.UserID == res.UserID);

                        foreach (Expert e in experts)
                        {
                            e.State = false;
                        }

                        // Update land
                        IQueryable<Land> lands = db.Lands.Where((Land la) => la.UserID == res.UserID);

                        foreach (Land l in lands)
                        {
                            l.State = false;
                        }

                        // Update zone
                        u.State = false;
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion